// 引入
import axios from "axios";
// import { useUserStore } from "@/store";
import nprogress from "nprogress";
//引入进度条样式
import "nprogress/nprogress.css";
//start:进度条开始 done:进度条结束
import { ElMessage, ElMessageBox } from 'element-plus'
// router
import router from '@/router';


let timer: any = null;

// 环境配置
const server = axios.create({
    timeout: 10000,
    baseURL: "/api",
});
// 请求拦截
server.interceptors.request.use((config: any) => {
    nprogress.start();
    let token = localStorage.getItem("token");
    if (config.url != "/api" && token != null && token != undefined && token != "") {
        config.headers.Authorization = token;
    }
    return config;
});
// 响应拦截
server.interceptors.response.use(
    (res) => {
        nprogress.done();
        // 判断是否为文件返回，是则直接返回数据
        if (res.headers.requesttype === 'file') {
            return res
        }
        // 状态响应成功
        const { data } = res
        // 返回成功状态码200
        if (data.code == "200") {
            return data;
            // 返回错误状态码500
        } else if (data.code == "500") {
            ElMessage({
                message: res.data.message,
                type: 'error',
            })
            // 返回失效状态码
        } else if (data.code == '401' || data.code == '402') {
            if (timer) {
                clearInterval(timer)
            }
            timer = setTimeout(() => {
                ElMessageBox.confirm(
                    data.message + '，是否重新登录',
                    '账号失效',
                    {
                        confirmButtonText: '登陆',
                        cancelButtonText: '取消',
                        type: 'warning',
                    }
                )
                    .then(() => {
                        router.push("/login")
                    })
                    .catch(() => {
                        ElMessage({
                            type: 'info',
                            message: 'Delete canceled',
                        })
                    })
            }, 500);
        }
    },
    (err) => {
        nprogress.done();
        // 该返回的数据则是axios.catch(err)中接收的数据
        ElMessage({
            message: '服务器找不到了',
            type: 'error',
        })
        return Promise.reject(err)
    }
);
// 添加请求
export const post = (url: any, params: any = {}, isFile = false) => {
    //默认无文件
    // 判断有没有文件
    let data: any = null;
    if (isFile) {
        // 有文件
        data = new FormData();
        for (let i in params) {
            data.append(i, params[i]);
        }
        return server({
            url,
            method: "post",
            data,
        });
    } else {
        return server({
            url,
            method: "post",
            data: params,
        });
    }
};
export const del = (url: any, params: any = {}) => {
    return server({
        url,
        method: "delete",
        data: params,
    });
};
// 修改请求
export const put = (url: any, params: any = {}, isFile = false) => {
    //默认无文件
    // 判断有没有文件
    let data: any = null;
    if (isFile) {
        // 有文件
        data = new FormData();
        for (let i in params) {
            data.append(i, params[i]);
        }
        return server({
            url,
            method: "put",
            data,
        });
    } else {
        return server({
            url,
            method: "put",
            data: params,
        });
    }
};
// get请求
export const get = (url: any, params = {}, isFile = false) => {
    if (isFile) {
        return server({
            url,
            method: "get",
            params,
            responseType: 'blob',//设置返回类型
        });
    }
    return server({
        url,
        method: "get",
        params,
    });
};